package com.ivncorp.evnproject.api;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.ivncorp.evnproject.model.UploadTable;
import com.ivncorp.evnproject.model.UserDetailsTable;


public class JSONParser {

	public JSONParser()
	{
	super();	
	}
	
	
	public ArrayList<UploadTable> parseCotDauNoi(JSONObject object)
	{
		ArrayList<UploadTable> arrayList=new ArrayList<UploadTable>();
		try {
			JSONArray jsonArray=object.getJSONArray("Value");
			for(int i=0;i<jsonArray.length();i++)
			{
				JSONObject jsonObj=jsonArray.getJSONObject(i);
				arrayList.add(new UploadTable(jsonObj.getInt("ID")*10,jsonObj.getString("MaCot"),jsonObj.getString("LoaiCot"),jsonObj.has("HinhDang")? jsonObj.getString("HinhDang"):"",  jsonObj.has("HinhDang2")? jsonObj.getString("HinhDang2"):"",jsonObj.has("HinhDang3")? jsonObj.getString("HinhDang3"):"",  "",  "", jsonObj.getString("Lat"), jsonObj.getString("Long"), 0,1));
			}
			
		
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Log.d("JSONParser => parseDepartment", e.getMessage());
		}
		return arrayList;
	}
	public ArrayList<UploadTable> parseTram(JSONObject object)
	{
		ArrayList<UploadTable> arrayList=new ArrayList<UploadTable>();
		try {
			JSONArray jsonArray=object.getJSONArray("Value");
			JSONObject jsonObj=null;
			for(int i=0;i<jsonArray.length();i++)
			{
				jsonObj=jsonArray.getJSONObject(i);
			
				arrayList.add(new UploadTable(jsonObj.getInt("ID"),jsonObj.getString("MaTram"),jsonObj.getString("TenTram"),  jsonObj.has("HinhTruoc")? jsonObj.getString("HinhTruoc"):"", jsonObj.has("HinhTrai")? jsonObj.getString("HinhTrai"):"",  jsonObj.has("HinhPhai")? jsonObj.getString("HinhPhai"):"", jsonObj.has("AnhTruoc")? jsonObj.getString("AnhTruoc"):"", jsonObj.has("AnhSau")? jsonObj.getString("AnhSau"):"", jsonObj.getString("Lat"), jsonObj.getString("Long"), Integer.parseInt(jsonObj.getString("CongSuat")),2));
			}
			
		
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Log.d("JSONParser => parseDepartment", e.getMessage());
		}
		return arrayList;
	}
	
	public ArrayList<UploadTable> parseTuTrungThe(JSONObject object)
	{
		ArrayList<UploadTable> arrayList=new ArrayList<UploadTable>();
		try {
			JSONArray jsonArray=object.getJSONArray("Value");
			JSONObject jsonObj=null;
			for(int i=0;i<jsonArray.length();i++)
			{
				jsonObj=jsonArray.getJSONObject(i);
				arrayList.add(new UploadTable(jsonObj.getInt("ID")*100,jsonObj.getString("TenTu"),jsonObj.getString("MaTu"),  jsonObj.getString("HinhDang"),  "",  "",  "",  "", jsonObj.getString("Lat"), jsonObj.getString("Long"), 0,3));
			}
			
		
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Log.d("JSONParser => parseDepartment", e.getMessage());
		}
		return arrayList;
	}
	public boolean parseSuccess(JSONObject object)
	{	boolean success=false;
			try {
				success= object.getBoolean("Value");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				Log.d("JSONParser => parseSuccess", e.getMessage());
			}
			
			return success;
	}
	
	public boolean parseUserAuth(JSONObject object)
	{	boolean userAtuh=false;
			try {
				userAtuh= object.getBoolean("Value");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				Log.d("JSONParser => parseUserAuth", e.getMessage());
			}
			
			return userAtuh;
	}
	
	public UserDetailsTable parseUserDetails(JSONObject object)
	{
		UserDetailsTable userDetail=new UserDetailsTable();
		
		try {
			JSONObject jsonObj=object.getJSONArray("Value").getJSONObject(0);
			
			userDetail.setFirstName(jsonObj.getString("firstName"));
			userDetail.setLastName(jsonObj.getString("lastName"));
			
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			Log.d("JSONParser => parseUserDetails", e.getMessage());
		}
		
		return userDetail;
			
	}
	
	
}
